package com.dinghui.mybatis.config;

import lombok.Data;

/**
 * <p>
 * 返回数据的封装类
 */
@Data
public class Result<T> {

    //状态码
    private String code;

    //提示信息
    private String result;

    private T data;

    private ResultStatus status;

    //新的token
    private String newToken;

    public Result(T data) {
        this.code = "200";
        this.result = "success";
        this.data = data;
        this.status = ResultStatus.OK;
    }

    public Result() {
        this.code = "200";
        this.result = "success";
        this.status = ResultStatus.OK;
    }

    private Result(CodeMsg codeMsg) {
        if (codeMsg == null) {
            return;
        }
        this.code = codeMsg.getCode();
        this.result = codeMsg.getMsg();
        this.status = ResultStatus.ERROR;
    }

    public Result(T data, String newToken) {
        this.code = "200";
        this.result = "success";
        this.data = data;
        this.status = ResultStatus.OK;
        this.newToken = newToken;
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(data);
    }
    public static <T> Result<T> success(T data, String newToken) {
        return new Result<T>(data,newToken);
    }

    public static <T> Result<T> error(CodeMsg codeMsg) {
        return new Result<T>(codeMsg);
    }

    /**
     * 返回状态
     */
    public enum ResultStatus {
        /**
         * 成功
         */
        OK,
        /**
         * 失败
         */
        ERROR
    }
}
